﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Yahoo_Compressor
{
    public partial class OptionsForm : Form
    {
        public OptionsForm()
        {
            InitializeComponent();
            cmbCssCompressionType.SelectedIndex = 0;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            txtJsMiniExtension.Text = Properties.Settings.Default.JavaScriptCompactExtension;
            chkJsDisableOptimizations.Checked = Properties.Settings.Default.JavaScriptDisableOptimizations;

            switch (Properties.Settings.Default.JavaScriptEncoding)
            {
                case "Unicode":
                    chkJsEncoding.SelectedIndex = 0;
                    break;
                case "UTF32":
                    chkJsEncoding.SelectedIndex = 1;
                    break;
                case "UTF8":
                    chkJsEncoding.SelectedIndex = 2;
                    break;
                case "UTF7":
                    chkJsEncoding.SelectedIndex = 3;
                    break;
                case "ASCII":
                    chkJsEncoding.SelectedIndex = 4;
                    break;
                default:
                    chkJsEncoding.SelectedIndex = 0;
                    break;
            }


            chkJsIsEvalIgnored.Checked = Properties.Settings.Default.JavaScriptIsEvalIgnored;
            chkJsIsObfuscateJavascript.Checked= Properties.Settings.Default.JavaScriptIsObfuscateJavascript;
            chkJsVerboseLogging.Checked = Properties.Settings.Default.JavaScriptIsVerboseLogging;
            txtJsLineBreakPosition.Value = Properties.Settings.Default.JavaScriptLineBreakPosition;
            chkJsPreserveAllSemicolons.Checked= Properties.Settings.Default.JavaScriptPreserveAllSemicolons;
            txtCssColumnWidth.Value = Properties.Settings.Default.StyleSheetColumnWidth;
            txtCssMiniExtension.Text= Properties.Settings.Default.StyleSheetCompactExtension;

            
            switch (Properties.Settings.Default.StyleSheetCssCompressionType)
            {
                case Yahoo.Yui.Compressor.CssCompressionType.StockYuiCompressor:
                    cmbCssCompressionType.SelectedIndex = 0;
                    break;
                case Yahoo.Yui.Compressor.CssCompressionType.Hybrid:
                    cmbCssCompressionType.SelectedIndex = 1;
                    break;
                case Yahoo.Yui.Compressor.CssCompressionType.MichaelAshRegexEnhancements:
                    cmbCssCompressionType.SelectedIndex = 2;
                    break;                
                default:
                    cmbCssCompressionType.SelectedIndex = 0;
                    break;
            }

            chkRemoveCssComments.Checked = Properties.Settings.Default.StyleSheetRemoveComments;

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            try
            {
                Properties.Settings.Default["JavaScriptCompactExtension"] = txtJsMiniExtension.Text;
                Properties.Settings.Default["JavaScriptDisableOptimizations"] = chkJsDisableOptimizations.Checked;

                switch (chkJsEncoding.SelectedIndex)
                {
                    case 0:
                        Properties.Settings.Default["JavaScriptEncoding"] = "Unicode";
                        break;
                    case 1:
                        Properties.Settings.Default["JavaScriptEncoding"] = "UTF32";
                        break;
                    case 2:
                        Properties.Settings.Default["JavaScriptEncoding"] = "UTF8";
                        break;
                    case 3:
                        Properties.Settings.Default["JavaScriptEncoding"] = "UTF7";
                        break;
                    case 4:
                        Properties.Settings.Default["JavaScriptEncoding"] = "ASCII";
                        break;
                    default:
                        Properties.Settings.Default["JavaScriptEncoding"] = "Unicode";
                        break;
                }


                Properties.Settings.Default["JavaScriptIsEvalIgnored"] = chkJsIsEvalIgnored.Checked;
                Properties.Settings.Default["JavaScriptIsObfuscateJavascript"] = chkJsIsObfuscateJavascript.Checked;
                Properties.Settings.Default["JavaScriptIsVerboseLogging"] = chkJsVerboseLogging.Checked;
                Properties.Settings.Default["JavaScriptLineBreakPosition"] = txtJsLineBreakPosition.Value;
                Properties.Settings.Default["JavaScriptPreserveAllSemicolons"] = chkJsPreserveAllSemicolons.Checked;
                Properties.Settings.Default["StyleSheetColumnWidth"] = txtCssColumnWidth.Value;
                Properties.Settings.Default["StyleSheetCompactExtension"] = txtCssMiniExtension.Text;

                switch (cmbCssCompressionType.SelectedIndex)
                {
                    case 0:
                        Properties.Settings.Default["StyleSheetCssCompressionType"] = Yahoo.Yui.Compressor.CssCompressionType.StockYuiCompressor;
                        break;
                    case 1:
                        Properties.Settings.Default["StyleSheetCssCompressionType"] = Yahoo.Yui.Compressor.CssCompressionType.Hybrid;
                        break;
                    case 2:
                        Properties.Settings.Default["StyleSheetCssCompressionType"] = Yahoo.Yui.Compressor.CssCompressionType.MichaelAshRegexEnhancements;
                        break;
                    default:
                        Properties.Settings.Default["StyleSheetCssCompressionType"] = Yahoo.Yui.Compressor.CssCompressionType.StockYuiCompressor;
                        break;
                }

                Properties.Settings.Default["StyleSheetRemoveComments"] = chkRemoveCssComments.Checked;

                Properties.Settings.Default.Save();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
